import torch
from tempo import *
from tempo0 import *  # 含有setParameters等函数


def read_mean_variance_from_csvfile(csv_file):
    # 读取CSV文件
    df_result = pd.read_csv(csv_file)

    # 提取均值和方差
    mean = df_result['mean'].dropna().values
    variance = df_result['variance'].dropna().values

    # 将均值和方差转换为numpy.ndarray
    mean_array = np.array(mean)
    variance_array = np.array(variance)

    return mean_array, variance_array


def torch_cov(input_vec):
    """ 计算一个batch的协方差
    Args:
        input_vec: shape = (bn, seq, 2)
    """
    m  = input_vec - torch.mean(input_vec, dim=1, keepdim=True)
    mT = torch.transpose(m, 1, 2)
    cov_matrix = torch.matmul(mT, m) / (m.shape[1] - 1)
    return cov_matrix.reshape(input_vec.shape[0], -1)


def set_and_load_parameters_pattern_0(parameters):
    """设置和加载功能一的参数"""
    setParameters(parameters)
    return {"root_path": parameters["root_path"],
            "win_length": parameters["win_length"],
            "win_stride": parameters["win_stride"]
            }


def set_and_load_parameters_pattern_1(parameters):
    """设置和加载功能一的参数"""
    setParameters(parameters)
    return {"trajdata_path": parameters["trajdata_path"],
            "win_length": parameters["win_length"],
            "win_stride": parameters["win_stride"]}


def set_and_load_parameters_pattern_2(parameters):
    """设置和加载功能一的参数"""
    setParameters(parameters)
    return {"scaler_path": parameters["scaler_path"],
            "model_save_path": parameters["model_save_path"],
            'encoder_file': parameters['encoder_file'],
            "n_estimators": parameters["n_estimators"],
            "booster": parameters["booster"],
            'learning_rate': parameters['learning_rate'],
            "gamma": 0,
            'max_depth': parameters['max_depth']
            }


def set_and_load_parameters_pattern_3(parameters):
    """设置和加载功能一的参数"""
    setParameters(parameters)
    return {"trajdata_path": parameters["trajdata_path"],
            "win_length": parameters["win_length"]}


def set_and_load_parameters_pattern_4(parameters):
    """设置和加载功能一的参数"""
    setParameters(parameters)
    return {"scaler_path": parameters["scaler_path"],
            "load_model_path": parameters["load_model_path"],
            'encoder_file': parameters['encoder_file']}



